﻿using OF.Lib.DataAccess.Entity;
using OF.Lib.Utility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;

namespace OF.Lib.DataAccess.Core
{
    public class SqlBuilderBase
    {
        #region 脚本处理
        protected Dictionary<string, object> m_WhereHasValueObjects = new Dictionary<string, object>();
        protected string PagedQuery_1_SelectSection { get; set; }
        protected string PagedQuery_2_TableSection { get; set; }
        protected string PagedQuery_3_Part { get; set; }
        protected string PagedQuery_4_WhereSection { get; set; }
        protected string PagedQuery_5_Part { get; set; }
        protected string PagedQuery_6_SortSection { get; set; }
        protected string PagedQuery_7_FootPart { get; set; }

        protected virtual void Init_PagedQuery(SQL sqlCfg)
        {
            Regex regex = new Regex(@"([\s\S]*)\#FROM\{\[([\s\S]*)\]\}([\s\S]*)\#WHERE\{\[([\s\S]*)\]\}([\s\S]*)\#SORT\{\[([\s\S]*)\]\}([\s\S]*)", RegexOptions.IgnoreCase);
            MatchCollection matches = regex.Matches(sqlCfg.Text);

            if (matches != null && matches.Count == 1)
            {
                PagedQuery_1_SelectSection = matches[0].Groups[1].Value;
                PagedQuery_2_TableSection = matches[0].Groups[2].Value;
                PagedQuery_3_Part = matches[0].Groups[3].Value;
                PagedQuery_4_WhereSection = matches[0].Groups[4].Value;
                PagedQuery_5_Part = matches[0].Groups[5].Value;
                PagedQuery_6_SortSection = matches[0].Groups[6].Value;
                PagedQuery_7_FootPart = matches[0].Groups[7].Value;
            }
        }

        protected virtual string BuildWhereSql(string whereSection, QueryFilter filter)
        {
            if (string.IsNullOrWhiteSpace(whereSection))
            {
                return string.Empty;
            }

            Type ft = filter.GetType();
            var props = ft.GetProperties(BindingFlags.Public | BindingFlags.Instance);

            foreach (PropertyInfo prop in props)
            {
                Type ptype = typeof(QueryFilter);
                if (prop.DeclaringType == ptype)
                {
                    continue;
                }
                var pvalue = prop.GetValue(filter, null);
                if (pvalue != null && !m_WhereHasValueObjects.ContainsKey(prop.Name.ToUpper()))
                {
                    m_WhereHasValueObjects.Add(prop.Name.ToUpper(), pvalue);
                }
            }
            string strWhere = Regex.Replace(whereSection, @"\<\?([\s\S]*?)\?\>",
                new MatchEvaluator(ParseWhereParameter), RegexOptions.Compiled | RegexOptions.IgnoreCase);
            return "WHERE " + strWhere;
        }

        protected virtual string ParseWhereParameter(Match match)
        {
            string matchValue = match.Value;

            Match m = Regex.Match(matchValue, @"@\w*", RegexOptions.IgnoreCase);
            string param = m.Value;
            param = param.Replace("@", "");

            string result = string.Empty;
            if (m_WhereHasValueObjects.ContainsKey(param.Trim().ToUpper()))
            {
                result = matchValue.Replace(@"<?", "").Replace(@"?>", "");
            }
            else
            {
                result = "1=1";
            }

            return result;
        }

        #endregion

 

    }
}
